{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "### COPIED FROM TRINITUM REPO ###\n",
    "\n",
    "import time\n",
    "from datetime import datetime, timedelta\n",
    "import tushare as ts\n",
    "import talib\n",
    "import os\n",
    "import time\n",
    "\n",
    "import requests\n",
    "\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib as mpl\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "# import quandl as quandl"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "本接口即将停止更新，请尽快使用Pro版接口：https://tushare.pro/document/2\n"
     ]
    }
   ],
   "source": [
    "df=ts.get_k_data('300580',start='2017-01-12',end='2017-05-26')\n",
    "closed=df['close'].values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1     2017-01-12\n",
       "2     2017-01-13\n",
       "3     2017-01-16\n",
       "4     2017-01-17\n",
       "5     2017-01-18\n",
       "         ...    \n",
       "85    2017-05-22\n",
       "86    2017-05-23\n",
       "87    2017-05-24\n",
       "88    2017-05-25\n",
       "89    2017-05-26\n",
       "Name: date, Length: 89, dtype: object"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['date']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "GDAX_TO_POLONIEX = {'BTC-USD': \"USDT_BTC\", 'LTC-USD': \"USDT_LTC\", 'ETH-USD': \"USDT_ETC\"}\n",
    "\n",
    "class Pipeline(object):\n",
    "\n",
    "\tdef __init__(self, interval): \n",
    "\t\tself.interval = interval\n",
    "\t\tself.POLO_URL = 'https://poloniex.com/public'\n",
    "\t\tself.POLO_HIST_DATA = self.POLO_URL + '?command=returnChartData&currencyPair=BTC_ETH&start=1410158341&end=1410499372'\n",
    "\n",
    "\tdef getCryptoHistoricalData(self, symbol, endTime, histPeriod, vwap=False):\n",
    "\n",
    "\t\tpoloniexJsonURL = self.POLO_HIST_DATA\n",
    "\n",
    "\t\timport json\n",
    "\t\timport requests\n",
    "\t\tpoloniexJson = requests.get(poloniexJsonURL).json()\n",
    "\n",
    "\t\tfrom pandas import DataFrame\n",
    "\t\thistDataframe = DataFrame.from_records(poloniexJson)\n",
    "\t\thistDataframe.drop('quoteVolume', axis=1, inplace=True)\n",
    "\t\thistDataframe.drop('weightedAverage', axis=1, inplace=True)\n",
    "\t\thistDataframe['date'] = histDataframe['date'].astype(float)\n",
    "\n",
    "\t\treturn histDataframe[[\"date\", \"open\", \"high\", \"low\", \"close\", \"volume\"]]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'dateToUNIX' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-9-36b9eb26c59d>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[0mpl\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mPipeline\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minterval\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m300\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mdf\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mpl\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mgetCryptoHistoricalData\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msymbol\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'BTC-USD'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mendTime\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'20180201'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mhistPeriod\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m30\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      3\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[0mrealtime_talib\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mIndicator\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[0mobv\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mIndicator\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdf\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'OBV'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[0mtrix\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mIndicator\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdf\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'TRIX'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m20\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m<ipython-input-8-7575aa6999a7>\u001b[0m in \u001b[0;36mgetCryptoHistoricalData\u001b[1;34m(self, symbol, endTime, histPeriod, vwap)\u001b[0m\n\u001b[0;32m      9\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     10\u001b[0m         \u001b[1;32mdef\u001b[0m \u001b[0mgetCryptoHistoricalData\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msymbol\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mendTime\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mhistPeriod\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mvwap\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mFalse\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 11\u001b[1;33m                 \u001b[0mendTimeUNIX\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mdateToUNIX\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mendTime\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     12\u001b[0m                 \u001b[0mstartDate\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mgetCurrentDateStr\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     13\u001b[0m                 \u001b[0mpriorDate\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mdatetimeDiff\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mstartDate\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mhistPeriod\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'dateToUNIX' is not defined"
     ]
    }
   ],
   "source": [
    "\n",
    "\tpl = Pipeline(interval=300)\n",
    "\tdf = pl.getCryptoHistoricalData(symbol='BTC-USD', endTime='20180201', histPeriod=30)\n",
    "\tfrom realtime_talib import Indicator\n",
    "\tobv = Indicator(df, 'OBV')\n",
    "\ttrix = Indicator(df, 'TRIX', 20)\n",
    "\trocr = Indicator(df, 'ROCR', 20)\n",
    "\tatr = Indicator(df, 'ATR', 20)\n",
    "\tprint(obv.getHistorical(), trix.getHistorical(), rocr.getHistorical(), atr.getHistorical())\n",
    "\ttest_ohlcv = [4000, 4100, 3900, 4200, 960]\n",
    "\tprint(obv.getRealtime(test_ohlcv), trix.getRealtime(test_ohlcv), rocr.getRealtime(test_ohlcv), atr.getRealtime(test_ohlcv))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "obv.getRealtime(test_ohlcv), trix.getRealtime(test_ohlcv), rocr.getRealtime(test_ohlcv), atr.getRealtime(test_ohlcv)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1     2017-01-12\n",
       "2     2017-01-13\n",
       "3     2017-01-16\n",
       "4     2017-01-17\n",
       "5     2017-01-18\n",
       "         ...    \n",
       "85    2017-05-22\n",
       "86    2017-05-23\n",
       "87    2017-05-24\n",
       "88    2017-05-25\n",
       "89    2017-05-26\n",
       "Name: date, Length: 89, dtype: object"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['date']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print('test')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "from realtime_talib import Indicator\n",
    "\n",
    "OHLCV = .... (pandas dataframe with stock data)\n",
    "rt_BBANDS = Indicator(OHLCV, \"BBANDS\", 2, 2, 5, 3)\n",
    "rt_MA = Indicator(OHLCV, \"AROON\", 2, 1)\n",
    "\t\n",
    "print(rt_MA.getHistorical(lag=1))\n",
    "tickData = (2800, 2700, 2600, 2900, 9394) # format of OHLCV\n",
    "print(rt_BBANDS.getRealtime(tickData, lag=2))\n",
    "print(rt_BBANDS.getRealtime(tickData, lag=2))\n",
    "\n",
    "...\n",
    "\n",
    " Terminal Outputs:\n",
    " [2320.1911391450003, 2322.398211915, ..., 2809.4632536955473]\n",
    " (2829.4632537420202, 2809.4632536955473, 2789.4632536490744)\n",
    " (2829.4632537420202, 2809.4632536955473, 2789.4632536490744)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
